package ChaiPiao;

import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

@WebServlet(name = "PurchaseSplitTestServiceServlet", urlPatterns = {"/purchase/purchaseSplitTestService"})
public class PurchaseSplitTestServiceServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doService(request, response);
    }

    private String readContent(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                (ServletInputStream) request.getInputStream(), "utf-8"));
        StringBuffer sb = new StringBuffer();
        String temp;
        while ((temp = br.readLine()) != null) {
            sb.append(temp);
        }
        br.close();
        return sb.toString();
    }

    protected void doService(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String content = readContent(request, response);
        String result = footballBasketballSplit(content);

        response.getWriter().write(result);
    }

    protected String footballBasketballSplit(String json) {
        boolean success = true;
        JSONObject root = new JSONObject();
        String error = "";
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("game");
            GameTicketSplitter gameTicketSplitter = new GameTicketSplitter();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject gameJsonObject = jsonArray.getJSONObject(i);
                Game game = new Game(gameJsonObject.getString("gameFlag"), gameJsonObject.getBoolean("dan"));
                JSONArray selectionJsonArray = gameJsonObject.getJSONArray("selection");
                for (int j = 0; j < selectionJsonArray.length(); j++) {
                    JSONObject selectionJsonObject = selectionJsonArray.getJSONObject(j);
                    String type = selectionJsonObject.getString("type");
                    JSONArray valueJsonArray = selectionJsonObject.getJSONArray("value");
                    for (int k = 0; k < valueJsonArray.length(); k++) {
                        game.addSelection(new GameSelection(type, valueJsonArray.getString(k)));
                    }
                }
                gameTicketSplitter.add(game);
            }

            String mixType = jsonObject.getString("mixType");
            root.put("mixType", mixType);
            MixTypeProvider mixTypeProvider = new MixTypeProvider();
            mixTypeProvider.setMixType(mixType);
            Integer base = mixTypeProvider.getMixBase();
            if (base == null) {
                success = false;
                error = "MIX_TYPE_NOT_SUPPORTED";
            } else {
                List<Note> notes = gameTicketSplitter.splitNote(base, mixTypeProvider.getMix_1());
                List<Ticket> tickets = gameTicketSplitter.splitTicket(base, mixTypeProvider.getMix_1());
                JSONObject data = new JSONObject();
                data.put("count", notes.size());

                JSONArray arrayNotes = new JSONArray();
                data.put("notes", arrayNotes);

                for (Note note : notes) {
                    arrayNotes.put(note.toString());
                }


                JSONArray arrayTickets = new JSONArray();
                data.put("tickets", arrayTickets);
                for (Ticket ticket : tickets) {
                    arrayTickets.put(ticket.toString());
                }

                root.put("data", data);
            }

        } catch (Exception e) {
            success = false;
            error = e.getMessage();
        }


        if (success) {
            root.put("success", true);
            return root.toString();
        }
        root = new JSONObject();
        root.put("success", false);
        root.put("error", error);
        return root.toString();
    }
}
